

Rem
bbdoc: Game2D entity manager
about: Holds, updates and renders entities.
endrem
Type TEntityManager

	'main list of all entities in the game
	Global _entities:TList = new TList

	'entities, per render layer
	'each index in the layer hold a tlist containing entities for that layer
	Global _layers:TBag = New TBag


	'removes all entities from the manager
	Function Clear()
		_entities.Clear()
		_layers.Clear()
	End Function


	Function AddEntity(e:TEntity, layer:Int)
		_entities.AddLast(e)

		Local l:TList = TList(_layers.Get(layer))
		If Not l
			l = New TList
			_layers.Set(layer, l)
		End If
		l.AddLast(e)
		e.SetRenderLayer(layer)
	End Function


	Function RemoveEntity(e:TEntity)
		_entities.Remove(e)

		Local l:TList = TList(_layers.Get(e.GetRenderLayer()))
		l.Remove(e)
	End Function


	Function Update()
		For local e:TEntity = eachin _entities
			e.Update()
		Next
	End Function


	'render entities in the renderlayers.
	'start with layer 0
	Function Render(tween:Double)
        Local amount:Int=0
		For Local index:Int = 0 To _layers.GetSize() - 1
			Local l:TList = TList(_layers.Get(index))
			If l
				For Local e:TEntity = EachIn l
					e.Render(tween)
	                amount:+1
				Next
			Endif
		Next
		?debug
        DrawText("entities " + amount, 0,0)
		?
	End Function

End Type